/**
 * @class   wosh::MessageQueue
 * @brief   The class implements a priority based queue of wosh::Message items.
 *
 * @see     wosh::BusRing
 ****************************************************************************
 * @version $Id: MessageQueue.h 2868 2010-08-07 21:13:01Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_MessageQueue_H__
 #define __WOSH_Core_MessageQueue_H__

 #include <woshDefs.h>
 #include <core/Message.h>
 #include <core/Mutex.h>
 #include <queue>


namespace wosh {


struct MessagePriorityComparison {
	bool operator()(Message *x, Message *y) {
		if ( x == NULL ) return false;
		if ( y == NULL ) return true;
		return x->getPriority() > y->getPriority();
	 }
};


class MessageQueue : public virtual IReflection
 {
	WOSH_CLASSNAME(wosh::MessageQueue)
	WOSH_CLASS_ABSTRACT(false)


	public:
		/**
		 * @brief  Empty constructor.
		 */
		MessageQueue() { }
		/**
		 * @brief  Destructor. Clear the queue.
		 */
		virtual ~MessageQueue() {
			clear();
		 }


	public:
		/**
		 * @brief  Extract the older message from the queue. FIFO. Atomic operation. Reentrant.
		 * @return the oldest message in the queue, NULL when empty.
		 * @note   Caller acquires the ownership and will have to free the object
		 */
		Message* pop();

		/**
		 * @brief  Append a message to the queue. FIFO. Atomic operation. Reentrant.
		 * @param [in] message the message to be added, ownership is assigned to the queue
		 * @note   Caller lose the ownership, it can't use the object anymore
		 */
		void push_back( Message* message );

		/**
		 * @brief  Clear the queue (removing all messages). Atomic operation. Reentrant.
		 */
		void clear();

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Evaluate if the queue is empty.
		 * @return false when there are messages in the queue; true else
		 */
		inline bool isEmpty() const		{ MutexLocker mL(&queueMux); return this->queueMsg.empty(); }

		/**
		 * @brief  Get the size of the queue.
		 * @return number of messages in the queue
		 */
		inline int size() const			{ MutexLocker mL(&queueMux); return this->queueMsg.size(); }

//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		std::priority_queue<Message*,
							std::vector<Message*>,
							MessagePriorityComparison> queueMsg;	///< messages' queue

		Mutex queueMux;								///< mutex of message queue

//@}


}; // class def

//		friend std::istream& operator>> (std::istream& i, Message& message);
//		friend std::ostream& operator<< (std::ostream& o, const Message& message);

}; // namespace wosh

#endif //__WOSH_Core_MessageQueue_H__
